www.gusucode.com > VC编写的串口调试软件 > VC编写的串口调试软件,内含Modbus协议类 支持对Modbus通讯调试/Modbus串口调试软件1.0/DlgChar.cpp
// DlgChar.cpp : implementation file // #include "stdafx.h" #include "ModbusRTU.h" #include "DlgChar.h" #include "DlgVerity.h" #include "Verity.h" #include "ConfigDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CDlgChar dialog CDlgChar::CDlgChar(CWnd* pParent /*=NULL*/) : CDialog(CDlgChar::IDD, pParent) { //{{AFX_DATA_INIT(CDlgChar) m_nTxtType = COM_HEXBIN; m_strSendTxt = _T(""); //}}AFX_DATA_INIT } void CDlgChar::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgChar) DDX_Radio(pDX, IDC_RADIO_ASCII, m_nTxtType); DDX_Text(pDX, IDC_EDIT_SEND, m_strSendTxt); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDlgChar, CDialog) //{{AFX_MSG_MAP(CDlgChar) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BTN_VERIFY, OnBtnVerify) //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_DCOM, &CDlgChar::OnBnClickedDcom) ON_BN_CLICKED(IDC_BTN_RCV, &CDlgChar::OnBnClickedBtnRcv) ON_BN_CLICKED(IDC_BTN_SEND, &CDlgChar::OnBnClickedBtnSend) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgChar message handlers void CDlgChar::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here delete this; } int CDlgChar::GetSendTxt(unsigned char * pucSendTxt) { UpdateData(TRUE); int nSendCount=0; if(COM_ASCII==m_nTxtType) { nSendCount=m_strSendTxt.GetLength () ; for(int nIndex=0;nIndex<nSendCount;nIndex++) { pucSendTxt[nIndex]=m_strSendTxt.GetAt (nIndex); } } if(COM_HEXBIN==m_nTxtType) { CVerity Verity; nSendCount=Verity.TransStrHex(m_strSendTxt,&m_ucHexBuffer[0],500); for(int nIndex=0;nIndex<nSendCount;nIndex++) { pucSendTxt[nIndex]=m_ucHexBuffer[nIndex]; } } return nSendCount; } void CDlgChar::OnCancel() { // TODO: Add extra cleanup here // CDialog::OnCancel(); } void CDlgChar::OnOK() { // TODO: Add extra validation here // CDialog::OnOK(); } void CDlgChar::OnBtnVerify() { // TODO: Add your control notification handler code here UpdateData(TRUE); CDlgVerity dlg; m_strSendTxt.TrimLeft (); m_strSendTxt.TrimRight (); dlg.m_strVerirtyChar=m_strSendTxt; if(dlg.DoModal()) { // if(!m_strSendTxt.IsEmpty ()) { m_strSendTxt = dlg.m_strVerirtyChar + _T(" ") + dlg.m_strVerityCode ; } UpdateData(FALSE); } } void CDlgChar::OnBnClickedDcom() { // TODO: 在此添加控件通知处理程序代码 //* CButton* pBtnWnd; pBtnWnd = (CButton*) GetDlgItem(IDC_DCOM); if(NULL == pBtnWnd) return; if( pBtnWnd->GetCheck () ) { CWnd* pWnd; pWnd = GetDlgItem(IDC_BTN_RCV); pWnd->EnableWindow (TRUE); pWnd = GetDlgItem(IDC_BTN_SEND); pWnd->EnableWindow (TRUE); pWnd = GetDlgItem(IDC_EDIT_SEND_TIME); pWnd->EnableWindow (TRUE); CWnd* pParendWnd = this->GetParent (); if ( NULL == pParendWnd ) return; //设定标题 CString strTitle; strTitle.Format("串口通讯 - 串口转发 "); pParendWnd->SetWindowText(strTitle); } else { CWnd* pWnd; pWnd = GetDlgItem(IDC_BTN_RCV); pWnd->EnableWindow (FALSE); pWnd = GetDlgItem(IDC_BTN_SEND); pWnd->EnableWindow (FALSE); pWnd = GetDlgItem(IDC_EDIT_SEND_TIME); pWnd->EnableWindow (FALSE); m_cRcvPort.StopMonitoring(); m_cSendPort.StopMonitoring(); CWnd* pParendWnd = this->GetParent (); if ( NULL == pParendWnd ) return; //设定标题 CString strTitle; strTitle.Format("串口通讯 "); pParendWnd->SetWindowText(strTitle); } } void CDlgChar::OnBnClickedBtnRcv() { // TODO: 在此添加控件通知处理程序代码 bool bResult = SetCom(m_cRcvPort, SP_RCV); SetTitle(bResult); } void CDlgChar::OnBnClickedBtnSend() { // TODO: 在此添加控件通知处理程序代码 bool bResult = SetCom(m_cSendPort,SP_SEND); SetTitle(bResult); } bool CDlgChar::SetCom(CSerialPort& cPort,int nType) { bool bResult = true; CConfigDlg* dlg = new CConfigDlg(this->GetParent (), cPort.GetDCB(),cPort.GetPortNum()); DWORD dwCommEvents = cPort.GetCommEvents(); dlg->m_strSendBuffer.Format("%d", cPort.GetWriteBufferSize()); dlg->m_CommBreakDetected = (dwCommEvents & EV_BREAK) > 0 ? TRUE : FALSE; dlg->m_CommCTSDetected = (dwCommEvents & EV_CTS) > 0 ? TRUE : FALSE; dlg->m_CommDSRDetected = (dwCommEvents & EV_DSR) > 0 ? TRUE : FALSE; dlg->m_CommERRDetected = (dwCommEvents & EV_ERR) > 0 ? TRUE : FALSE; dlg->m_CommRingDetected = (dwCommEvents & EV_RING) > 0 ? TRUE : FALSE; dlg->m_CommRLSDDetected = (dwCommEvents & EV_RLSD) > 0 ? TRUE : FALSE; dlg->m_CommRxchar = TRUE; dlg->m_CommRxcharFlag = (dwCommEvents & EV_RXFLAG) > 0 ? TRUE : FALSE; dlg->m_CommTXEmpty = (dwCommEvents & EV_TXEMPTY) > 0 ? TRUE : FALSE; if (dlg->DoModal() == IDOK) { cPort.ClosePort (); dwCommEvents = 0; if (dlg->m_CommBreakDetected) dwCommEvents |= EV_BREAK; if (dlg->m_CommCTSDetected) dwCommEvents |= EV_CTS; if (dlg->m_CommDSRDetected) dwCommEvents |= EV_DSR; if (dlg->m_CommERRDetected) dwCommEvents |= EV_ERR; if (dlg->m_CommRingDetected) dwCommEvents |= EV_RING; if (dlg->m_CommRLSDDetected) dwCommEvents |= EV_RLSD; if (dlg->m_CommRxchar) dwCommEvents |= EV_RXCHAR; if (dlg->m_CommRxcharFlag) dwCommEvents |= EV_RXFLAG; if (dlg->m_CommTXEmpty) dwCommEvents |= EV_TXEMPTY; int nPortNum = atoi(dlg->m_strPort) ; UINT uStopBits = 1; if("1" == dlg->m_strStopBits) uStopBits = 1; //ONESTOPBIT==0; //1 stop bit else if("1.5" == dlg->m_strStopBits) uStopBits = ONE5STOPBITS; //ONE5STOPBITS==1;//1.5 stop bits else if("2" == dlg->m_strStopBits) uStopBits = TWOSTOPBITS; //TWOSTOPBITS==2//2 stop bits else uStopBits = 1; //ONESTOPBIT; //1 stop bit dwCommEvents = EV_RXCHAR | EV_CTS; if(cPort.InitPort(this->GetParent ()->m_hWnd , nPortNum, atoi(dlg->m_strBaudRate), dlg->m_strParity[0], atoi(dlg->m_strDataBits), uStopBits,//atoi(dlg->m_strStopBits), dwCommEvents, atoi(dlg->m_strSendBuffer))) { cPort.StartMonitoring(); } else { bResult = false; } } delete dlg; return bResult; } void CDlgChar::SetTitle(bool bOpen) { CWnd* pParendWnd = this->GetParent (); if ( NULL == pParendWnd ) return; if ( bOpen) { //* // CWnd* pWnd; pWnd = pParendWnd->GetDlgItem(IDC_BTN_STOPREV); pWnd->SetWindowText ("正在接收"); // m_bStopRev = true; pWnd = pParendWnd->GetDlgItem(IDC_BTN_SEND); pWnd->EnableWindow (TRUE); pWnd = pParendWnd->GetDlgItem(IDC_CHECK_AUTOSEND); pWnd->EnableWindow (TRUE); //设定标题 CString strTitle; strTitle.Format("串口通讯 - 接收串口%2d <> 转发串口%2d ",m_cRcvPort.GetPortNum (),m_cSendPort.GetPortNum ()); pParendWnd->SetWindowText(strTitle); } else { //设定标题 CString strTitle; strTitle.Format("串口通讯"); pParendWnd->SetWindowText(strTitle); MessageBox("初始化串口失败!\n再试试!"); CWnd* pWnd; pWnd = pParendWnd->GetDlgItem(IDC_BTN_SEND); pWnd->EnableWindow (FALSE); pWnd = pParendWnd->GetDlgItem(IDC_CHECK_AUTOSEND); pWnd->EnableWindow (FALSE); //*/ } }